home *** CD-ROM | disk | FTP | other *** search
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/stat.h>
- #include <sys/errno.h>
- #include "crtlocal.h"
- #include <Aliases.h>
-
- long root_parID;
-
- FSSpec getparent(long parID)
- {
- FSSpec name;
- OSErr err;
- CInfoPBRec cPB;
- cPB.dirInfo.ioDrParID = parID;
- do
- {
- /* get information about dir */
- cPB.hFileInfo.ioCompletion = (ProcPtr)0L;
- cPB.hFileInfo.ioNamePtr = (StringPtr)name.name;
- cPB.hFileInfo.ioVRefNum = crt_ioVRefNum;
- cPB.hFileInfo.ioFDirIndex = -1;
- cPB.hFileInfo.ioDirID = cPB.dirInfo.ioDrParID;
-
- err = PBGetCatInfoSync(&cPB);
- }
- while (0);
- if (err) *name.name = 0;
- name.parID = cPB.dirInfo.ioDrParID;
- name.vRefNum = crt_ioVRefNum;
- return name;
- }
-
- FSSpec hfs_canon(long crt_parID, const char *nam, int follow)
- {
- char *nxt;
- FSSpec canon;
- long parID;
- char name[255];
- int err;
- Boolean targetIsFolder,wasAliased;
- struct stat statbuf;
- strcpy(name, nam);
- if (*name=='/')
- {
- parID = root_parID;
- strcpy(name, &name[1]);
- if (!*name) return getparent(root_parID);
- }
- else
- {
- parID = crt_parID;
- if (!*name)
- {
- *canon.name = 0;
- return canon;
- }
- }
- do
- {
- nxt = strchr(name, '/');
- if (nxt)
- {
- FSSpec canon;
- *nxt = 0;
- canon = hfs_canon(parID, name, 1);
- err = macstat(canon.name, &statbuf, canon.vRefNum, canon.parID);
- if (err)
- {
- *canon.name = 0;
- return canon;
- }
- if (S_ISDIR(statbuf.st_mode))
- {
- parID = statbuf.st_ino;
- }
- else
- {
- errno = ENOTDIR;
- *canon.name = 0;
- return canon;
- }
- strcpy(name, ++nxt);
- }
- }
- while (nxt);
- if (!*name) return getparent(parID);
- if (*name == '.')
- {
- if (!name[1])
- return getparent(parID);
- if ((name[1]=='.') && !name[2])
- {
- canon = getparent(parID);
- return getparent(canon.parID);
- }
- }
- *canon.name = strlen(name);
- BlockMove(name, &canon.name[1], *canon.name);
- canon.parID = parID;
- canon.vRefNum = crt_ioVRefNum;
- if (follow) err = ResolveAliasFile(&canon,true,&targetIsFolder,&wasAliased);
- return canon;
- }
-
- int chdir(char *name)
- {
- struct stat statbuf;
- int err;
- if (!*name) return 0;
- err = stat(name, &statbuf);
- if (S_ISDIR(statbuf.st_mode))
- {
- crt_parID = statbuf.st_ino;
- return 0;
- }
- errno = ENOTDIR;
- return -1;
- }
-
- int fchdir(int fd)
- {
- if (crt_fd_tab[fd].flags & O_CATALOG)
- {
- crt_parID = crt_fd_tab[fd].fd;
- return 0;
- }
- return -1;
- }
-